1674F - Desktop Rearrangement - CodeForces Solution


data structures greedy implementation *1800

Please click on ads to support us..

Python Code:

import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

n, m, q = [int(x) for x in input().split()]
board = [["."]*m for i in range(n)]
stars = 0
for i in range(n):
    board[i] = [x for x in input().decode().strip()]
    stars += board[i].count("*")
stararea = 0
cellsy = 0
cellsx = 0
while cellsx*n+cellsy < stars:
    if board[cellsy][cellsx] == "*": stararea += 1
    cellsy += 1
    if cellsy%n == 0:
        cellsy = 0
        cellsx += 1

for j in range(q):
    x, y = [int(z)-1 for z in input().split()]
    if board[x][y] == "*":
        if board[(stars-1)%n][(stars-1)//n] == "*":
            stararea -= 1
        if x+y*n < stars-1: 
            stararea -= 1
        board[x][y] = "."
        stars -= 1
    elif board[x][y] == ".":
        if board[stars%n][stars//n] == "*":
            stararea += 1
        if x+y*n <= stars: 
            stararea += 1
        board[x][y] = "*"
        stars += 1
    print(stars - stararea)
    


        

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,q;
    cin>>n>>m>>q;
    char arr[n+2][m+2];
     vector < int >mp(m + 1, 0);
    int cnt=0;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='*')
            {
                mp[j]++;
                cnt++;
            }
        }
    }
    while(q--)
    {
        int x,y;
        cin>>x>>y;
        if(arr[x][y]=='.')
        {
            arr[x][y]='*';
            cnt++;
            mp[y]++;
        }
        else
        {
            arr[x][y]='.';
            cnt--;
            mp[y]--;
        }
        int ans=0;
        for(int i=1; i<=(cnt/n); i++)
        {
            int p=n-mp[i];
            ans+=p;
        }
        int col=cnt%n;
        for(int i=1; i<=n; i++)
        {
            if (col== 0) break;
            if (arr[i][(cnt/n) + 1] == '.')
            {
                ans ++;
            }
            col --;
        }
        cout<<ans<<endl;;
    }
}


Comments

Submit
0 Comments
More Questions

HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation